package com.letv.android.client.activity;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

import com.letv.ads.util.LogInfo;
import com.letv.android.client.LetvSDK;
import com.letv.android.client.constant.LetvSDKConstant;
import com.letv.android.young.client.R;
import com.letv.core.bean.IVideo;

public class LetvSDKPlayActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.letv_sdk_main);

        LetvSDK.getInstance().init();
    }

    @Override
    protected void onStart() {
        super.onStart();
        final IVideo video1 = new IVideo();
        final IVideo video2 = new IVideo();
        final IVideo video3 = new IVideo();

        LetvSDK.getInstance().registerCallBack(mCallback);// 注册回调

        findViewById(R.id.bt_playvideo).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // 单视频
                video1.setmVid(getVid());
//                video1.setmCurrentTime(4607000);
                LogInfo.log("sguotao", "--->>>play video vid: " + video1.getmVid());
                LetvSDK.getInstance().play(LetvSDKPlayActivity.this, video1,false,null);
            }
        });

        /**
         * 获取下载地址
         */
        findViewById(R.id.bt_download).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                video3.setmVid(getVid());
                LetvSDK.getInstance().parseRefVidByThirdParty(LetvSDKPlayActivity.this,video3);
            }
        });

        /**
         * 播放本地视频
         */
        findViewById(R.id.bt_playurl).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                video3.setmTitle("家和万事兴01");
                video3.setmFilePath("/sdcard/Letv/storage/download/23975074.mp4");
                LetvSDK.getInstance().play(LetvSDKPlayActivity.this, video3, true,null);
            }
        });
    }

    @Override
    protected void onStop() {
        super.onStop();
//        LetvSDK.getInstance().onAppExit();
    }

    private long getVid() {
        long vid = 20529381;// 学姐知道
        EditText etVid = (EditText) this.findViewById(R.id.et_vid);
        String strVid = etVid.getText().toString();
        if (!TextUtils.isEmpty(strVid)) {
            try {
                vid = Long.parseLong(strVid);
            } catch (NumberFormatException e) {
                vid = 20529381;
                e.printStackTrace();
            }
        }
        return vid;
    }

    private LetvSDK.LetvSDKCallback mCallback = new LetvSDK.LetvSDKCallback() {

        @Override
        public void onEvent(int event, final String name, IVideo video) {
            if(event == LetvSDKConstant.EVENT_PLAY_START){
                Log.i("LetvSDK"," start!");
            }
            else if(event == LetvSDKConstant.EVENT_PLAY_RESUME){
                Log.i("LetvSDK"," resume!");
            }
            else if(event == LetvSDKConstant.EVENT_PLAY_STOP){
                Log.i("LetvSDK"," stop!");
            }
            else if(event == LetvSDKConstant.EVENT_PLAY_NEXT){
                Log.i("LetvSDK"," play next!");
            }
            else if(event == LetvSDKConstant.EVENT_PLAY_PAUSE){
                Log.i("LetvSDK"," pause!" + "title:"+video.getmTitle() +"\n total time:"+ video.getmTotalTime()+"\n currentTime:"+video.getmCurrentTime());
            }
            else if(event == LetvSDKConstant.EVENT_START_DOWNLOAD){
                Log.i("LetvSDK","download:"+video.getmDownloadUrl());
                Toast.makeText(LetvSDKPlayActivity.this,video.getmDownloadUrl(),Toast.LENGTH_SHORT).show();
            }
        }
    };
}
